package cn.com.dashboard.controller;

import cn.com.dashboard.entity.ZrRules;
import cn.com.dashboard.model.*;
import cn.com.dashboard.model.rules.*;
import cn.com.dashboard.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author jiaming
 */
@Controller
@RequestMapping("/zhurong/rule")
public class RuleController {

    @Autowired
    private RuleService ruleService;

    @RequestMapping("/manager")
    public Object ruleManager() {
        return "rule/rule";
    }

    @RequestMapping("/list")
    @ResponseBody
    public DataTableResponse<ZrRules> list(@RequestParam(value = "draw", defaultValue = "1") int draw,
                                           @RequestParam(value = "start", defaultValue = "1") int start,
                                           @RequestParam(value = "length", defaultValue = "10") int pageSize,
                                           @RequestParam(value = "order[0][column]", required = false) Integer orderIndex,
                                           @RequestParam(value = "order[0][dir]", required = false) String orderDir) {
        QueryRulesListRequest request = new QueryRulesListRequest();
        request.setPageNo(start);
        request.setPageSize(pageSize);
        CommonPage<ZrRules> data = ruleService.queryRulesList(request);
        DataTableResponse response = new DataTableResponse();
        response.setData(data.getList());
        response.setRecordsTotal(data.getTotalCount().intValue());
        response.setRecordsFiltered(data.getTotalCount().intValue());
        response.setDraw(draw + 1);
        return response;
    }

    @RequestMapping("/details")
    @ResponseBody
    public CommonResult<RulesDetailResponse> details(@RequestBody @Validated QueryRulesDetails request) {
        return ruleService.details(request);
    }

    @PostMapping("/add")
    public CommonResult<Boolean> addRule(@RequestBody @Validated AddRuleRequest request) {
        return ruleService.addRule(request);
    }

    @PostMapping("/delete")
    public CommonResult<Boolean> delete(@RequestBody @Validated DeleteRuleRequest request) {
        return ruleService.deleteRule(request);
    }

    @PostMapping("/edit")
    public CommonResult<Boolean> edit(@RequestBody @Validated EditRuleRequest request) {
        return ruleService.editRule(request);
    }

}
